﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapSurface.UIModelElement;
using MapSurface.AssemblyReflexion;
using System.Windows.Controls;
using System.Reflection;
using Microsoft.Surface.Presentation;

namespace MapSurface.WidgetSurface
{
    public class EditModelLoad
    {
        public EditModelLoad()
        {
        }
        #region EditMode
        public void visitEditMode(ref Object rootUI, Object context)
        {
            if (rootUI != null)
            {
                rootUI = setEventsSelect(rootUI, context);
                if (isItemControl(rootUI))
                    for (int i = 0; i < ((ItemsControl)rootUI).Items.Count; i++){
                        object o=((ItemsControl)rootUI).Items.GetItemAt(i);
                        visitEditMode(ref o, context);
                    }
                else if (isPanel(rootUI))
                    for (int i = 0; i < ((Panel)rootUI).Children.Count; i++)
                    {
                        Object o=((Panel)rootUI).Children[i];
                       visitEditMode(ref o, context);
                    }
                else if (isContentControl(rootUI))
                {
                    object o = ((ContentControl)rootUI).Content;
                    visitEditMode(ref o, context);
                }
            }
        }

        private Object setEventsSelect(Object element, Object context)
        {
            try
            {
                setHandler(element, "ContactMove", context, "Drag_Button_MouseMove",Microsoft.Surface.Presentation.Contacts.ContactChangedEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            try
            {
                setHandler(element, "ContactDown", context, "Drag_Button_MouseDown",Microsoft.Surface.Presentation.Contacts.ContactDownEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

            }
            try
            {
                setHandler(element, "ContactLeave", context, "Drag_Button_MouseLeave",Microsoft.Surface.Presentation.Contacts.ContactLeaveEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

            }

            try
            {
                setHandler(element, "SizeChanged", context, "handlerSized", Microsoft.Surface.Presentation.Contacts.ContactChangedEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);

            }

            return element;

        }
        private void setHandler(Object instance, String evenName, Object context, String handler,Object route)
        {
            try
            {

                Type t = instance.GetType();
                System.Reflection.MethodInfo mi = context.GetType().GetMethod(handler);
                //Object w = Activator.CreateInstance(context.GetType());
                Type t2 = typeof(ContactEventHandler);
                var d = EventHandler.CreateDelegate(t2, context, mi);
                //t.GetEvent(evenName).AddEventHandler(instance, d);
                System.Reflection.MethodInfo[] m = instance.GetType().GetMethods();
                     
                foreach (MethodInfo i in m)
                {
                    if (i.Name.CompareTo("AddHandler") == 0)
                    {
                        Object[] param = {route , d };
                        i.Invoke(instance, param);
                        break;
                    }
                }

            }
            catch (Exception e)
            {
#if DEBUG
                Console.WriteLine("[DEBUG] " + e.Message);
#endif
            }
        }

        #endregion
        public Boolean isItemControl(Object element)
        {
            try
            {

                ItemsControl obj = (ItemsControl)element;
                return true;
            }
            catch (InvalidCastException e)
            {
#if DEBUG
                Console.Out.WriteLine("[DEBUG] isItemControl " + e.Message);
#endif


            }
            return false;
        }
        public Boolean isPanel(Object element)
        {
            try
            {
                Panel p = (Panel)element;
                return true;
            }
            catch (InvalidCastException e)
            {
#if DEBUG
                Console.Out.WriteLine("[DEBUG] isPanel " + e.Message);
#endif


            }
            return false;
        }
        public Boolean isContentControl(Object element)
        {
            try
            {
                ContentControl c = (ContentControl)element;
                return true;
            }
            catch (InvalidCastException e)
            {
#if DEBUG
                Console.Out.WriteLine("[DEBUG] isContentControl " + e.Message);
#endif
            }
            return false;
        }
        public Boolean hasChild(Object element)
        {
            try
            {
                Type t = element.GetType();
                System.Reflection.MethodInfo mi = t.GetMethod("GetVisualChild", BindingFlags.Instance | BindingFlags.NonPublic);
                if (mi != null)
                    return true;
            }
            catch (Exception e)
            {
#if DEBUG
                Console.Out.WriteLine("[DEBUG] hasChild " + e.Message);
#endif

            }
            return false;

        }


    }
}
